// U型布局数据格式
const LEFT_TYPE = 'left'
const RIGHT_TYPE = 'right'
const anchorPoints = [
  [0, 0.5],
  [0.5, 0],
  [1, 0.5],
  [0.5, 1]
]
// 根据工位数获取初始化数据
export const getCanvasDataU = (len) => {
  if (!len)
    return {
      nodes: [],
      edges: []
    }
  const arr = new Array(len).fill({})
  const leftLen = Math.ceil(arr.length / 2)
  const rightLen = arr.length - leftLen
  const leftNodes = []
  const leftEdges = []
  for (let i = 0; i < leftLen; i++) {
    const leftNode = {
      id: `l_u_${i}`,
      label: `L__${i}`,
      sort: i + 1,
      cluster: LEFT_TYPE,
      anchorPoints: anchorPoints,
      username: '',
      deviceName: '',
      deviceSn: '',
      type: 'kingCard'
    }
    leftNodes.push(leftNode)

    if (i < leftLen - 1) {
      const lEdge = {
        source: leftNode.id,
        target: `l_u_${i + 1}`,
        style: {
          endArrow: true
          // stroke: '#33a5e2'
        },
        type: 'line',
        sourceAnchor: 3,
        targetAnchor: 1,
        idx: 'l' + i,
        autoDraw: false
      }
      leftEdges.push(lEdge)
    }
  }
  const rightNodes = []
  const rightEdges = []
  for (let i = 0; i < rightLen; i++) {
    const rightNode = {
      id: `r_u_${i}`,
      label: `R__${i}`,
      sort: i + leftLen + 1,
      cluster: RIGHT_TYPE,
      anchorPoints: anchorPoints,
      username: '',
      deviceName: '',
      deviceSn: '',
      type: 'kingCard'
    }
    rightNodes.push(rightNode)
  }
  rightNodes.reverse()
  rightNodes.forEach((_, idx) => {
    if (idx < rightNodes.length - 1) {
      const rEdge = {
        source: `r_u_${idx}`,
        target: `r_u_${idx + 1}`,
        type: 'line',
        style: {
          endArrow: true
        },
        sourceAnchor: 1,
        targetAnchor: 3,
        idx: 'r' + idx,
        autoDraw: false
      }
      rightEdges.push(rEdge)
    }
  })
  let connectEdge = {}
  if (leftNodes.length && rightNodes.length) {
    connectEdge = {
      source: leftNodes[leftNodes.length - 1].id,
      target: rightNodes[rightNodes.length - 1].id,
      type: 'polyline',
      style: {
        endArrow: true,
        radius: 4,
        offset: 12
      },
      sourceAnchor: 2,
      targetAnchor: leftNodes.length === rightNodes.length ? 0 : 3,
      idx: 'connect',
      autoDraw: false
    }
  }
  return {
    nodes: [...leftNodes, ...rightNodes],
    edges: [...leftEdges, connectEdge, ...rightEdges]
  }
}
